package com.powernode.controller;

import com.powernode.dto.PageParam;
import com.powernode.dto.Result;
import com.powernode.entity.IndexCarousel;
import com.powernode.service.IndexCarouselService;
import com.powernode.service.IndexContentService;
import com.powernode.util.MinioUtil;
import com.powernode.validation.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/cms/admin/indexCarousels")
public class AdminIndexCarouselController {

    @Autowired
    private IndexCarouselService indexCarouselService;
    @Autowired
    private MinioUtil minioUtil;

    @GetMapping
    public Result list(@Validated PageParam param){
        return Result.success(indexCarouselService.pageQuery(param));
    }

    @PostMapping
    public Result save(@RequestBody @Validated IndexCarousel entity){
        indexCarouselService.saveIndexCarousel(entity);
        return Result.success("新增首页轮播图成功",entity.getId());
    }

    @PutMapping
    public Result update(@RequestBody @Validated(UpdateGroup.class) IndexCarousel entity){
        return Result.success("修改首页轮播图成功",indexCarouselService.updateIndexCarousel(entity));
    }

    @DeleteMapping("/{indexCarouselId}")
    public Result remove(@PathVariable Long indexCarouselId){
        return Result.success("删除首页轮播图成功",indexCarouselService.removeIndexCarousel(indexCarouselId));
    }

    /**
     * 上传首页轮播图
     **/
    @PostMapping("/img/upload")
    public Result upload(MultipartFile file){
        return minioUtil.upload(file,"carousel");
    }

    /**
     * 删除首页轮播图
     **/
    @DeleteMapping("/img/remove/{filename}")
    public Result removeFile(@PathVariable String filename){
        return minioUtil.removeFile(filename);
    }
}
